package com.pkk.penguin.frame.know.java.Javacoreknowledge.transients.util;

import cn.hutool.core.io.FileUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @description: transient工具类
 * @author: peikunkun
 * @create: 2019-12-16 14:24
 **/
public class TransientUtil {


  /**
   * @Description: 序列化
   * @Param: [object]
   * @return: void
   * @Author: peikunkun
   * @Date: 2019/12/16 0016 下午 3:02
   */
  public static void seranlizable(Object object) {
    ObjectOutputStream os = null;
    try {
      final File tempFile = FileUtil.file("C:/serializable.txt");
      os = new ObjectOutputStream(new FileOutputStream(tempFile));
      os.writeObject(object);
      os.flush();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        os.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }


  /**
   * @Description: 不序列化
   * @Param: [classs]
   * @return: T
   * @Author: peikunkun
   * @Date: 2019/12/16 0016 下午 3:04
   */
  public static Object unSeranlizable() {
    Object t = null;
    ObjectInputStream is = null;
    try {
      final File tempFile = new File("C:/serializable.txt");
      is = new ObjectInputStream(new FileInputStream(tempFile));
      t = (Object) is.readObject();
    } catch (IOException | ClassNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        is.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return t;
  }
}
